/**
 * Created by banglong on 16-4-28.
 */
// 添加onload事件队列
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != "function") {
        window.onload = func;
    } else {
        window.onload = function () {
            oldonload();
            func();
        }
    }
}

// 存入cookie的值
function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + encodeURI(value) +
        ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

// 获取cookie中的值
function getCookie(c_name) {
    if (document.cookie.length > 0) {
        var c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            var c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return decodeURI(document.cookie.substring(c_start, c_end))
        }
    }
    return ""
}

//定义公司信息 vm 对象
var vmCompanyInfo = avalon.define({
    $id: "company-info",
    data: {
        logoImg: "",
        companyNameCn: "",
        systemName: "",
        companyNameEn: ""
    }
});

//定义用户 vm 对象
var vmUserInfo = avalon.define({
    $id: "user-info",
    data: {
        username: "",   //用户名
        password: "",   //密码
        authCode: "",   //验证码
        authCodeUrl: "checkCode/service.do",   //验证码链接
        rememberMe: false,  //是否记住我
        loginFailed: false, //登录失败
        msg: "" //失败原因
    },
    refresh: refresh    //刷新验证码
});

function refresh() {
    vmUserInfo.data.authCodeUrl += "?" + Math.random();
}

//登录操作
function formSubmit(e) {
    e.preventDefault();
    if (vmUserInfo.data.username.replace(/(^\s*)|(\s*$)/g, "") == "") {
        layer.msg("请输入用户名");
        return false;
    }
    if (vmUserInfo.data.password.replace(/(^\s*)|(\s*$)/g, "") == "") {
        layer.msg("请输入密码");
        return false;
    }
    if (vmUserInfo.data.authCode.replace(/(^\s*)|(\s*$)/g, "") == "") {
        layer.msg("请输入验证码");
        return false;
    }
    var url = "user/login.do";
    // var url = "testJson/loginSuccess.json";
    $.ajax({
        url: url,
        type: "POST",
        data: {
            username: vmUserInfo.data.username,
            password: vmUserInfo.data.password,
            authCode: vmUserInfo.data.authCode
        },
        dataType: "json",
        success: function (data) {
            //登录成功
            if (data.resultcode == 200) {
                //记住我功能
                if (vmUserInfo.data.rememberMe == true) setCookie("loginName", vmUserInfo.data.username, 7);
                //存入cookie
                // if (data.result.userID) setCookie("userID", data.result.userID, 7);
                // if (data.result.username) setCookie("username", data.result.username, 7);
                // if (data.token) setCookie("token", data.token, 7);
                // if (data.result.headImg) setCookie("headImg", data.result.headImg, 7);
                //记住我功能
                // if (vmUserInfo.data.rememberMe == true) localStorage.setItem("loginName", vmUserInfo.data.username);
                //存入localStorage
                if (data.result.userID) localStorage.setItem("userID", data.result.userID);
                if (data.result.username) localStorage.setItem("username", data.result.username);
                if (data.token) localStorage.setItem("token", data.token);
                if (data.result.headImg) localStorage.setItem("headImg", data.result.headImg);
                setTimeout(function () {
                    window.location = "home.jsp";
                }, 500);
            } else {
                vmUserInfo.data.password = "";
                vmUserInfo.data.authCode = "";
                vmUserInfo.data.msg = data.reason;
                vmUserInfo.data.loginFailed = true;
                refresh();
            }
        }
    });
}

//获取登录页的配置信息
function loadLoginConfig() {
    //todo 更改url
    $.getJSON("testJson/config.json", function (data) {
        if (data.resultcode == 200) {
            vmCompanyInfo.data = data.result;
        }
    });
}
addLoadEvent(loadLoginConfig);

//记住用户名
function rememberMe() {
    var loginName = getCookie("loginName");
    if (loginName == null) return false;
    if (loginName.replace(/(^\s*)|(\s*$)/g, "") == "") return false;
    vmUserInfo.data.username = loginName;
    vmUserInfo.data.rememberMe = true;
}
addLoadEvent(rememberMe);